<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/9
 * Time: 15:06
 */

namespace app\admin\model;


use app\admin\helper\LangHelper;
use app\admin\helper\SearchHelper;
use app\admin\popo\LayData;
use think\Config;
use think\Session;

class LogsM extends BaseM
{
    protected $table = 'tb_logs';
    protected $module = '日志中心';

    /**
     * 前端search控件
     * button vale [ add edit del search export ]
     * @return mixed
     */
    public function __search(){
        $search = [
            'tag'=>['field'=>'tag','title'=>'__tag__','type'=>'input','value'=>''],
            'created_at'=>['field'=>'created_at','title'=>'__created_at__','type'=>'date_range','value'=>'function:getMonthDateRange'],
            'button'=>['type'=>'button','value'=>['search','export']],
        ];
        SearchHelper::convertValue($search);
        $search['form'] = implode(',',array_keys($search));
        return LangHelper::convertLang($search,'field');
    }

    /**
     * 前端表格列
     * @return string
     */
    public function __header(){
        $header = [
            ['field'=>'op_uid','title'=>'__op_uid__'],
            ['field'=>'op_module','title'=>'__op_module__'],
            ['field'=>'op_type','title'=>'__op_type__'],
            ['field'=>'op_origin','title'=>'__op_origin__'],
            ['field'=>'op_data','title'=>'__op_data__'],
            ['field'=>'op_ip','title'=>'__op_ip__'],
            ['field'=>'op_sql','title'=>'__op_sql__'],
            ['field'=>'created_at','title'=>'__created_at__'],
        ];
        return json_encode(LangHelper::convertLang($header,'field'));
    }

    /*
     * 新增
     * */
    public static function add($log){
        $mod = new self();
        $log['created_at'] = time();
        $mod->save($log);
    }

    /*
     * 列表
     * */
    public function listData(){
        $page=input("post.page/d");
        $limit=input("post.limit/d");
        $tag=input("post.tag/s");
        $time=input("post.created_at/s");
        $start = strtotime(explode(' - ',$time)[0]);
        $end = strtotime(explode(' - ',$time)[1]);
        $where = [
            'op_uid|op_type|op_data|op_origin|op_ip|op_module'=>['like',"%".$tag."%"],
            'created_at'=>['between', [$start, $end+86399]],
        ];
        $log = [
            'op_uid' => Session::get('username'),
            'op_module' => $this->module,
            'op_type' => 'search',
            'op_origin' => $this->table,
            'op_data' => json_encode($where,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
            'op_ip' => getIP(),
        ];
        //export逻辑
        if (input("post.export/b")) {
            $header = json_decode($this->__header(),true);
            $export_data = $this->where($where)->order('id desc')->select()->toArray();
            $log['op_type'] = 'export';
            $log['op_data'] = json_encode($header,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
            $log['op_sql'] = $this->getLastSql();
            LogsM::add($log);
            pr_log($log,'operate');
            export('日志列表',$export_data,$header);
        }

        $resp = new LayData();
        $resp->count = $this->where($where)->count();
        $resp->data = $this->where($where)->order('id desc')->page($page,$limit)->select();
        $resp->extra_data = Config::get('app_debug')?$this->getLastSql():'';
        $log['op_sql'] = $this->getLastSql();
        LogsM::add($log);
        pr_log($log,'operate');
        return $resp;
    }

}